package com.bjesc_b2c.app.appraiset.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.adapter.AllorderAdapter;
import com.bjesc_b2c.app.appraiset.bean.CompleteOrderBean;
import com.bjesc_b2c.app.appraiset.mvp.presenter.CompleteOrderPresenter;
import com.bjesc_b2c.app.appraiset.mvp.presenter.impl.CompleteOrderPresenterImpl;
import com.bjesc_b2c.app.appraiset.mvp.view.CompleteOrderView;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.AllOrderActivity;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.CustomerDetailsActivity;
import com.bjesc_b2c.app.appraiset.utils.CustomSharedPreferences;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.rxretrofit_build.model.RxBusBean;
import com.rxretrofit_build.util.MapUtil;

import java.util.ArrayList;
import java.util.List;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.CommonAdapter;
import butterknife.Bind;
import rxretrofit_base.model.KeyBean;

/**
 * Created by yidong on 2017/8/8.
 */

public class BaseAllOrderFragment extends BaseFragment implements SpringView.OnFreshListener, CompleteOrderView, CommonAdapter.OnItemClickListener {
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.spring_view)
    SpringView springView;
    public CompleteOrderPresenter orderPresenter;
    private AllorderAdapter allorderAdapter;
    private int status ;
    public int page = 1;
    public int type = 1;
    public String keyWord = "";
    public boolean isLodmore = true;
    public boolean isShowDialog = true;
    private List<CompleteOrderBean> completeList = new ArrayList<CompleteOrderBean>();
    private Intent mIntent;
    public boolean isPrepared;
    //标志当前页面是否可见
    public boolean isVisible;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.fragment_todayplan, null);
        return contentView;
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isPrepared = true;
        lazyLoad();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        orderPresenter = new CompleteOrderPresenterImpl(this);
        //懒加载
        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }

    protected void onVisible() {
        lazyLoad();
    }

    protected void lazyLoad() {

    }

    protected void onInvisible() {

    }

    @Override
    public void initView() {
        mIntent = new Intent();
        recyclerView.setBackgroundResource(R.color.bg);
        springView.setListener(this);
        springView.setHeader(new DefaultHeader(mContext));
        springView.setFooter(new DefaultFooter(mContext));
        springView.setType(SpringView.Type.FOLLOW);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
    }

    @Override
    public void initData() {

    }

    public void setSatus(int sta) {
        status = sta;
    }

    @Override
    public void loadStart() {
        if (isShowDialog)
        showLoading();
    }

    @Override
    public void loadCompleted() {
        cancelLoading();
        if (springView != null) {
            springView.onFinishFreshAndLoad();
        }
    }

    @Override
    public void loadFailure() {
        cancelLoading();
        if (springView != null) {
            springView.onFinishFreshAndLoad();
        }
    }

    @Override
    public void getOrderList(List<CompleteOrderBean> bean) {

        if (bean != null && bean.size() > 0) {
            refreshData(bean);
            isLodmore = true;
        } else {
            showToast("没有了");
            if (page == 1) {
                allorderAdapter = null;
                recyclerView.setAdapter(allorderAdapter);
            }
            isLodmore = false;
        }
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {

        }
    }

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        CustomSharedPreferences.saveString(mContext, new MapUtil()
                .put(KeyBean.ORDER_ID.valueOf(), completeList.get(position).getDispatch_id())
                .build(), KeyBean.FILE_NAME.valueOf());
        mIntent.setClass(mContext, CustomerDetailsActivity.class);
        startIntent(mIntent);
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void refreshData(List<CompleteOrderBean> data) {
        if (page == 1) {
            completeList.clear();
        }
        completeList.addAll(data);
        if (allorderAdapter == null) {
            allorderAdapter = new AllorderAdapter(mContext, R.layout.layout_item_all_order, completeList);
            allorderAdapter.setOnItemClickListener(this);
            recyclerView.setAdapter(allorderAdapter);
        } else {
            allorderAdapter.refresh(completeList);
        }
    }


    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        if (isLodmore) {
            page++;
            orderPresenter.getAllOrderList(mContext, page, type, keyWord, status);
        }else {
            springView.onFinishFreshAndLoad();
        }
        isShowDialog = false;
    }

    /**
     * 上拉加载
     */
    @Override
    public void onLoadmore() {
        if (isLodmore) {
            page++;
            orderPresenter.getAllOrderList(mContext, page, type, keyWord, status);
        }else {
            springView.onFinishFreshAndLoad();
        }
        isShowDialog = false;
    }

    @Override
    protected void doOnNext(RxBusBean rxBusBean) {
        super.doOnNext(rxBusBean);
        if (rxBusBean.getRequestMark().equals(AllOrderActivity.class.getName())) {
            if (!isVisible || !isPrepared) {
                return;
            }else if (rxBusBean.getO() instanceof Intent) {
                isShowDialog = true;
                page = 1;
                type = ((Intent) rxBusBean.getO()).getIntExtra("type", 1);
                keyWord = ((Intent) rxBusBean.getO()).getStringExtra("keyWord");
                orderPresenter.getAllOrderList(mContext, page, type, keyWord, status);
            }
        } else if (rxBusBean.getRequestMark().equals("keyWord")) {
            keyWord = rxBusBean.getO().toString();
        }
    }
}
